import 'package:flutter/material.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import '../widgets/AppColors.dart';

enum SwitchingMode { day, month }

class TimeSwitching extends StatefulWidget {
  final SwitchingMode mode;
  final Function(DateTime) onDateChanged;

  TimeSwitching({
    Key? key,
    required this.mode,
    required this.onDateChanged,
  }) : super(key: key);

  @override
  _TimeSwitchingState createState() => _TimeSwitchingState();
}

class _TimeSwitchingState extends State<TimeSwitching> {
  late DateTime _selectedDate;

  @override
  void initState() {
    super.initState();
    _selectedDate = DateTime.now();
  }

  void _changeDate(int value) {
    setState(() {
      DateTime newDate;
      if (widget.mode == SwitchingMode.day) {
        newDate = _selectedDate.add(Duration(days: value));
      } else {
        newDate = DateTime(_selectedDate.year, _selectedDate.month + value, 1);
      }

      // 检查新日期是否大于当前日期
      if (newDate.isBefore(DateTime.now()) ||
          newDate.isAtSameMomentAs(DateTime.now())) {
        _selectedDate = newDate;
        widget.onDateChanged(_selectedDate);
      } else {
        TDToast.showText('不可大于当前时间', context: context);
      }
    });
  }

  String _formatDate() {
    if (widget.mode == SwitchingMode.day) {
      return '${_selectedDate.year}-${_selectedDate.month.toString().padLeft(2, '0')}-${_selectedDate.day.toString().padLeft(2, '0')}';
    } else {
      return '${_selectedDate.year}-${_selectedDate.month.toString().padLeft(2, '0')}';
    }
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        IconButton(
          icon: Icon(Icons.arrow_left),
          onPressed: () => _changeDate(-1),
        ),
        GestureDetector(
          onTap: () {
            TDPicker.showDatePicker(context, title: '选择时间',
                onConfirm: (selected) {
              DateTime newDate;
              if (widget.mode == SwitchingMode.day) {
                newDate = DateTime(
                  selected['year']!,
                  selected['month']!,
                  selected['day']!,
                ); // 解析选中的日期
              } else {
                newDate = DateTime(
                  selected['year']!,
                  selected['month']!,
                  1,
                ); // 处理为月份
              }
              widget.onDateChanged(newDate); // 回传选中的日期
              setState(() {
                _selectedDate = newDate; // 更新选中的日期以回显
              });
              Navigator.of(context).pop();
            }, useDay: widget.mode == SwitchingMode.day, dateStart: [
              2023,
              1,
              1
            ], dateEnd: [
              DateTime.now().year,
              DateTime.now().month,
              DateTime.now().day
            ], initialDate: [
              2024,
              1,
              1
            ]);
          },
          child: Text(
            _formatDate(),
            style: TextStyle(fontSize: 18, color: AppColors.textPrimary),
          ),
        ),
        IconButton(
          icon: Icon(Icons.arrow_right),
          onPressed: () => _changeDate(1),
        ),
      ],
    );
  }
}

class $ {}
